/*
 *  Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved.
 * 
 *       http://stripbandunk.com/
 * 
 *  STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package stripbandunk.tutorial.treetabledemo.model;

import java.util.ArrayList;
import java.util.List;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
import stripbandunk.tutorial.treetabledemo.entity.Category;
import stripbandunk.tutorial.treetabledemo.entity.Product;

/**
 *
 * @author Eko Kurniawan Khannedy
 */
public class ProductTreeTableModel extends AbstractTreeTableModel {

    private final String[] columns = new String[]{"Id", "Name", "Price"};

    private List<Category> categories;

    public ProductTreeTableModel(List<Category> categories) {
        super("List of Product");
        this.categories = categories;
    }

    public ProductTreeTableModel() {
        this(new ArrayList<Category>(0));
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    public int getColumnCount() {
        return columns.length;
    }

    public Object getValueAt(Category category, int column) {
        switch (column) {
            case 0:
                return category.getName();
            default:
                return null;
        }
    }

    public Object getValueAt(Product product, int column) {
        switch (column) {
            case 0:
                return product.getCode();
            case 1:
                return product.getName();
            case 2:
                return product.getPrice();
            default:
                return null;
        }
    }

    public Object getValueAt(Object node, int column) {
        if (node instanceof Category) {
            Category category = (Category) node;
            return getValueAt(category, column);
        } else if (node instanceof Product) {
            Product product = (Product) node;
            return getValueAt(product, column);
        } else {
            switch (column) {
                case 0:
                    return node.toString();
                default:
                    return null;
            }
        }
    }

    public Product getChild(Category category, int index) {
        return category.getProducts().get(index);
    }

    public Object getChild(Object parent, int index) {
        if (parent instanceof Category) {
            Category category = (Category) parent;
            return getChild(category, index);
        } else if (parent == getRoot()) {
            return categories.get(index);
        } else {
            return null;
        }
    }

    public int getChildCount(Object parent) {
        if (parent == getRoot()) {
            return categories.size();
        } else if (parent instanceof Category) {
            Category category = (Category) parent;
            return category.getProducts().size();
        } else {
            return 0;
        }
    }

    public int getIndexOfChild(Object parent, Object child) {
        if (parent == getRoot()) {
            return categories.indexOf(child);
        } else if (parent instanceof Category) {
            Category category = (Category) parent;
            return category.getProducts().indexOf(child);
        } else {
            return 0;
        }
    }
}
